package com.ziq.zcamp.place

import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.facebook.imagepipeline.request.ImageRequest
import com.ziq.base.extension.click
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.post.PostingDetail
import com.ziq.zcamp.databinding.ItemPlaceDetailPunchRecommendBinding
import com.ziq.zcamp.databinding.ItemPlaceDetailPunchRecommendMoreBinding
import com.ziq.zcamp.util.ImageUtil

class PlaceDetailPunchRecommendGroup(val adapter: PlaceDetailPunchRecommendAdapter) : DetailGroup() {

    override fun getHeaderViewType(groupPosition: Int): Int {
        return 0
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return if((list?.size?:0) > showMoreLimit){
            if((getChildrenCount() - 1) == childPosition){
                PlaceDetailPunchRecommendAdapter.TYPE_MORE
            } else {
                PlaceDetailPunchRecommendAdapter.TYPE_POST
            }
        } else {
            PlaceDetailPunchRecommendAdapter.TYPE_POST
        }
    }

    private var list : List<PostingDetail>? = null
    val showMoreLimit = 5
    fun setData(datalist: List<PostingDetail>) {
        list = datalist
    }

    override fun getChildrenCount(): Int {
        var result = list?.size?:0
        if(result > showMoreLimit){
            return showMoreLimit + 1
        }
        return result
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return false
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    override fun onBindHeaderViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }

    override fun onBindFooterViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        val viewType = getChildViewType(groupPosition, childPosition)
        when (viewType) {
            PlaceDetailPunchRecommendAdapter.TYPE_POST -> {
                onBindPost(holder, groupPosition, childPosition)
            }
            PlaceDetailPunchRecommendAdapter.TYPE_MORE -> {
                onBindSubjectMore(holder, groupPosition, childPosition)
            }
        }
    }

    private fun onBindPost(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ){
        holder?.let {
            val binding = ItemPlaceDetailPunchRecommendBinding.bind(it.itemView)
            if(list != null){
                val item = list?.get(childPosition)
                binding.root.click {
                    if(item != null){
                        adapter.adapterAction?.itemClick(item)
                    }
                }
                var content = ""
                if(item?.post?.title?.isNotEmpty() == true){
                    content = "${item.post?.title}"
                }else if(item?.post?.content?.isNotEmpty() == true){
                    content = "${item.post?.content}"
                } else if(item?.post?.p_type == 1){
                    content = "分享图片"
                } else if(item?.post?.p_type == 2){
                    content = "分享视频"
                } else if(item?.post?.p_type == 2){
                    content = "分享图片"
                }
                binding.tvText.text = content

                ImageUtil.updateImageFromNetwork(binding.avator, item?.user?.header_url, ImageRequest.CacheChoice.DEFAULT)

                val imageInfo = item?.post?.images?.get(0)
                if(imageInfo != null){
                    ImageUtil.updateImageFromNetwork(binding.image, imageInfo.url, ImageRequest.CacheChoice.DEFAULT)
                }
            }
        }
    }

    private fun onBindSubjectMore(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ){
        holder?.let {
            val binding = ItemPlaceDetailPunchRecommendMoreBinding.bind(it.itemView)
            binding.root.click {
                adapter.adapterAction?.moreClick()
            }
        }
    }


}